local SkillExcel 	= require("excel.Skill").Skill
local Timer		 	= require("core.Timer")
local random	 	= math.random
local SkillDefine	= require("skill.Define")
local Skill			= require("skill.Skill")

local EQUIP_CALL_SKILL_CONDITION_HIT	= SkillDefine.EQUIP_CALL_SKILL_CONDITION_HIT
local EQUIP_CALL_SKILL_CONDITION_BE_HIT = SkillDefine.EQUIP_CALL_SKILL_CONDITION_BE_HIT

-- ����װ���ı�
function update(obj)
	local equipCallSkillData = obj.equipCallSkillData
	if equipCallSkillData then
		for _,conditionSkillList in pairs(equipCallSkillData) do
			conditionSkillList[0] = 0
		end
	end
	
	local equip = obj.db.equip
	for k = 1, ItemDefine.EQUIP_LEN do
		local grid = equip[k]
		if grid then
			local condition = getValue(grid.id,"condition")
			if condition>0 then
				if not equipCallSkillData then
					equipCallSkillData = {}
					obj.equipCallSkillData = equipCallSkillData
				end
				
				local conditionSkillList = equipCallSkillData[condition]
				
				if not conditionSkillList then
					conditionSkillList = {[ 0 ] = 0 }
					equipCallSkillData[condition] = conditionSkillList
				end
			
				local len	  = conditionSkillList[0] + 1
				local data	  = conditionSkillList[len]
				local skillID = getValue(grid.id, "callSkillID")
				
				if not data then
					data = { }
					conditionSkillList[len] = data
				end
				data.callSkillID = skillID
				data.rand		 = getValue(grid.id, "rand")
				data.coolDown	 = SkillExcel[skillID].coolDown
				
				conditionSkillList[ 0 ]	= len
			end
		end
	end
	
	obj.goodEquipCallSkillData = RefineAttrAdd.getGoodEquipConf(obj)
end

function callSkill(obj,target,condition,tx,ty, skillID, isPet)
		
	local userData  = Skill.getUserData(obj)
	local x, y	    = obj.x, obj.y
	local now	    = Timer.now
	local conf		= obj.goodEquipCallSkillData
	
	--��װ�ӳ�
	if conf then
		if condition == EQUIP_CALL_SKILL_CONDITION_BE_HIT then	--����
			if conf.beHitSkillID ~=0 and random(1, 100) <= conf.beHitSkillRate then
				-- ���CD �����޷�ʹ�ü���
				userData.skillLastTime		 = 0
				userData.normalSkillLastTime = 0
				Skill.use(obj,conf.beHitSkillID,tx, ty, x, y, target)
			end
		elseif condition == EQUIP_CALL_SKILL_CONDITION_HIT then	--����
			if conf.atackSkillID ~= 0 and random(1, 100) <= conf.atackRate then
				-- ���CD �����޷�ʹ�ü���
				userData.skillLastTime		 = 0
				userData.normalSkillLastTime = 0
				Skill.use(obj,conf.atackSkillID,tx, ty, x, y, target)
			end
		end
	end
	
	-- ʥ����װ
	Gem2.checkSuitEffect3(obj, target, tx, ty, skillID, condition, isPet)
	
	-- װ�����ü���	
	local equipCallSkillData = obj.equipCallSkillData
	if not equipCallSkillData then return end 
	local conditionSkillList = equipCallSkillData[condition]
	if not conditionSkillList then return end
	local len = conditionSkillList[0]
	if len<1 then return end
	
	for i=1,len do
		local data = conditionSkillList[i]
		if (not data.lastUseTime or now - data.lastUseTime >= data.coolDown) and random(1, 100) <= data.rand then
			-- ���CD �����޷�ʹ�ü���
			data.lastUseTime			 = now
			userData.skillLastTime		 = 0
			userData.normalSkillLastTime = 0
			Skill.use(obj, data.callSkillID ,tx, ty, x, y, target)
		end
	end	
end

function onHumanUseSkill(human,target,condition,tx,ty, skillID)
	local now = Timer.now
	local equipCallSkillLastUseSkillTime = human.equipCallSkillLastUseSkillTime
	if not equipCallSkillLastUseSkillTime or now - equipCallSkillLastUseSkillTime>1000 then
		human.equipCallSkillLastUseSkillTime = now
		callSkill(human,target,condition,tx,ty,skillID)
	end
end